package cn.edu.zut.cs.Controller;

import cn.edu.zut.cs.Business.FirendsService;
import cn.edu.zut.cs.Business.UserService;
import cn.edu.zut.cs.Model.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    FirendsService firendsService;

    /**
     * 登录
     * @param session
     * @param model
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/toLogin")
    public String login(HttpSession session, Model model, @RequestParam("usernam") String username, @RequestParam("password") String password){
        if(username==null||username=="") {
            model.addAttribute("msg", "请输入用户名！");
            return "login";
        }
        if(password==null||password=="") {
            model.addAttribute("msg", "请输入密码！");
            return "login";
        }
        //查找登录用户
        User user=userService.findbyusernameandpwd(username);
        /*System.out.println(user);
        System.out.println("..."+password+"**");*/
        if (user==null) {
            model.addAttribute("msg", "用户名或密码错误！");
            return "login";
        }
        if(password.equals(user.getPwd())){
            session.setAttribute("userInfo",user);
            session.setAttribute("urli",user.getHeadimage());
            return "main";
        }
        model.addAttribute("msg","用户名或密码错误！");
        return "login";
    }

    /**
     * 注册用户
     */
    @RequestMapping("register")
    public String Loginup(Model model,@RequestParam("logupname") String username,@RequestParam("logupPhone") String phone,@RequestParam("loguppwd") String password)
    {
        //查询是否已存在该用户
        User user=userService.findbyusernameandpwd(username);
        if (user==null){
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String str = simpleDateFormat.format(date);
            /*String str=date.toString();*/
            userService.zhuceuser(username,password,str,phone);
            System.out.println(username+" "+password+" "+str+" "+phone+" "+"注册成功！");
            model.addAttribute("msgg","注册成功！");
            return "login";
        }

        model.addAttribute("msgg","该用户已存在！");
        return "login";
    }

    /**
     * 完善信息
     */
    @RequestMapping("updateinfo")
    public String updateinfo(HttpSession session,Model model,@RequestParam("imageurl") String imageurl,@RequestParam("nickname") String nickname,@RequestParam("localselect") String localselect,@RequestParam("sex") String sex,@RequestParam("aboutme") String aboutme){
        System.out.println("完善的信息为："+imageurl+" "+nickname+" "+localselect+" "+sex+" "+aboutme);
       User user=(User) session.getAttribute("userInfo");
        userService.updateuserinfo(nickname,user.getUsername(),imageurl,aboutme,localselect,sex);
        user.setNickname(nickname);
        session.setAttribute("userInfo",user);
        return "personpage";
    }

    /**
    * 找回密码
    * */
    @RequestMapping("reset")
    public String reset(Model model,@RequestParam("logupname") String username,@RequestParam("logupPhone") String phone,@RequestParam("loguppwd") String password)
    {
        //查询是否已存在该用户
        User user=userService.findbyusernameandpwd(username);
        /*System.out.println(user.getPhone()+" "+phone);*/
        if (user==null){
            model.addAttribute("msggg","尚未注册！请注册！");
            return "re-password";
        }
        if(!(user.getPhone().equals(phone))) {
            model.addAttribute("msggg", "手机号与用户名不匹配！");
            return "re-password";
        }
        userService.updatepwd(username,password);
        model.addAttribute("msggg","修改完成！");
        return "login";
    }

    /**
     * 跳转重置密码页面
     */
    @RequestMapping("/forgetpwd")
    public String forgetpwd(){
        return "re-password";
    }

    /**
     * 退出登录
     */
    @RequestMapping("/outLogout")
    public String logout(HttpSession session,Model model){
        //获取用户
        User user=(User) session.getAttribute("userInfo");
        String username=user.getUsername();
        //获取退出时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = simpleDateFormat.format(date);
        userService.logout(username,str);
        session.invalidate();
        model.addAttribute("ytc","已退出！");
        return "login";
    }

    @RequestMapping(value = "/getUsername",produces = "text/html; charset=UTF-8")
    @ResponseBody
    public String getUsername(HttpSession session){
        User userInfo=(User)session.getAttribute("userInfo");
        String username = userInfo.getNickname();
        System.out.println("当前登录用户昵称: "+username);
        return username;
    }

    @RequestMapping("/listFriends")
    @ResponseBody
    public List<String> listFriends(HttpSession session){
        User userInfo=(User)session.getAttribute("userInfo");
        String username = userInfo.getUsername();
        List<String> fList = firendsService.listFriends(username);
        return fList;
    }

    @RequestMapping("/userImg")
    @ResponseBody
    public String getUserImg(HttpSession session){
        User user = (User)session.getAttribute("userInfo");
        String imgUrl=user.getHeadimage();
        return imgUrl;
    }
}
